package com.ww.springboot.boot.thread.study.base.lock.aqs.Demo1;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：
 * 场景1:测试 中断是否会改变同步队列中的顺序
 *
 * @author 🧑 ‍wanwei
 * @since 2023-02-13 10:18
 */
public class Main22 {

    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(()->{
            lock.lock();
            System.out.println("线程1开始");
            try {
                Thread.sleep(1000);
                System.out.println("线程1结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        });

        Thread thread2 = new Thread(()->{
            try {
                //进入同步队列后 出队列时依旧调用lock方法
                lock.lock();
            } catch (Exception e) {
                System.out.println("线程2被打断");
                e.printStackTrace();
            }
            System.out.println("线程2开始");
            try {
                Thread.sleep(1000);
                System.out.println("线程2结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        });

        Thread thread3 = new Thread(()->{
            lock.lock();
            System.out.println("线程3开始");
            try {
                Thread.sleep(1000);
                System.out.println("线程3结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        });


        thread.start();
        thread2.start();
        thread3.start();
        Thread.sleep(100);

        thread2.interrupt();

    }
}
